const inquirer = require("inquirer");

const { exec } = require("child_process");

let changeToCustomerInputItem = [
  {
    name: `[ False ] - Not Install This Package.`,
    value: false,
    short: `false`
  },
  {
    name: `[ False ] - Customer Input.`,
    value: 'to_cus',
    short: 'to_cus'
  },
]

// npm dist-tag electron
function fetchElectronVersions() {
  let pkgName = 'electron'
  return new Promise((resolve, reject) => {
    exec(`npm dist-tags ls ${pkgName}`, (err, res) => {
      if (err) reject(err);
      if (typeof res === "string") {
        res = res.split("\n").reverse();
      }
      let list = [], unLinkList = ['unsupported', 'nightly', 'beta', 'head'], defaultVersionCode = "";
      // 迭代
      res.map(item => {
        if (!item || '') return;
        for(let s of unLinkList) {
          if(new RegExp(s, 'ig').test(item)) return;
        }
        let [name, code] = item.replace(" ", "").split(":");
        // 不重复添加默认版本
        if(defaultVersionCode === code) {
          return false
        }
        if ("latest" === name) defaultVersionCode = code;
        //
        list.push({
          name: `${name} : ^${code}`,
          value: `^${code}`,
          short: `^${code}`
        });
      });
      list = list.splice(0, 3)
      list.push(...changeToCustomerInputItem);
      resolve({
        pkgName,
        defaultVersionCode,
        defaultVersion: `^${defaultVersionCode}`,
        list
      });
    });
  });
}

function fetchElectronBuilderVersions() {
  let pkgName = 'electron-builder'
  return new Promise((resolve, reject) => {
    let defaultVersionCode = '21.2.0'
    let list = [
      {
        name: `Default latst.(${pkgName}@${defaultVersionCode})`,
        value: `^${defaultVersionCode}`,
        short: `^${defaultVersionCode}`
      }
    ]
    list.push(...changeToCustomerInputItem);
    resolve({
      pkgName,
      defaultVersionCode,
      defaultVersion: `^${defaultVersionCode}`,
      list
    });
  });
}

function fetchElectronDebugVersions() {
  let pkgName = 'electron-debug';
  return new Promise((resolve, reject) => {
    let defaultVersionCode = '3.0.1'
    let list = [
      {
        name: `Default latst.(${pkgName}@${defaultVersionCode})`,
        value: `^${defaultVersionCode}`,
        short: `^${defaultVersionCode}`
      }
    ]
    list.push(...changeToCustomerInputItem);
    resolve({
      pkgName,
      defaultVersionCode,
      defaultVersion: `^${defaultVersionCode}`,
      list
    });
  });
}

async function init() {
  let electronListObj = await fetchElectronVersions();
  let electronBuilderListObj = await fetchElectronBuilderVersions();
  let electronDebugListObj = await fetchElectronDebugVersions();

  inquirer
    .prompt([
      {
        name: "appId",
        type: "input",
        message: `Input 'appId'：`,
        default: "com.test.app"
      },
      {
        name: "productName",
        type: "input",
        message: `Input 'productName'：`,
        default: "default app"
      },
      // electron@
      {
        name: "eleVersion",
        type: "list",
        message: "Choose Electron Version：",
        default: electronListObj.defaultVersion,
        choices: electronListObj.list
      },
      {
        name: "eleVersion",
        type: "input",
        message: "Input Electron Version：",
        default: electronListObj.defaultVersion,
        when(asked) {
          return asked.eleVersion === 'to_cus';
        }
      },
      // electron-builder@
      {
        name: "eleBuilderVersion",
        type: "list",
        message: "Choose electron-builder Version：",
        default: electronBuilderListObj.defaultVersion,
        choices: electronBuilderListObj.list
      },
      {
        name: 'eleBuilderVersion',
        type: "input",
        message: "Input electron-builder Version：",
        default: electronBuilderListObj.defaultVersion,
        when(asked) {
          return asked.eleBuilderVersion === 'to_cus';
        }
      },
      // electron-debug@
      {
        name: "eleDebugVersion",
        type: "list",
        message: "Choose electron-debug Version：",
        default: electronDebugListObj.defaultVersion,
        choices: electronDebugListObj.list
      },
      {
        name: 'eleDebugVersion',
        type: "input",
        message: "Input electron-debug Version：",
        default: electronDebugListObj.defaultVersion,
        when(asked) {
          return asked.eleDebugVersion === 'to_cus';
        }
      },
      // devtools type
      {
        name: 'devtoolsInstallType',
        type: "rawlist",
        message: "Choose devtoolsInstallType：",
        default: 'none',
        choices: [
          {
            name: 'Dowload from Google server.',
            value: 'chrome',
            short: 'chrome'
          },
          {
            name: 'Customer File Path.',
            value: 'file',
            short: 'file'
          },
          {
            name: 'None.',
            value: 'none',
            short: 'none'
          }
        ]
      }
    ])
    .then(answers => {
      console.log(answers);
    });
}
init();
